Skip to content

fix: restore unreachable no-default-class error in load_plugin - #2011

Open
manunicholasjacob wants to merge 1 commit into
NVIDIA:mainfrom
manunicholasjacob:fix/plugins-default-class
Open

fix: restore unreachable no-default-class error in load_plugin#2011
manunicholasjacob wants to merge 1 commit into
NVIDIA:mainfrom
manunicholasjacob:fix/plugins-default-class

Conversation

@manunicholasjacob

Copy link
Copy Markdown

What this changes

load_plugin() reads generator_mod.DEFAULT_CLASS directly when a plugin is
specified as category.module. If the module does not define that attribute,
the attribute access raises AttributeError, so the else branch below it
cannot be reached:

if generator_mod.DEFAULT_CLASS:
    plugin_class_name = generator_mod.DEFAULT_CLASS
else:
    raise ValueError(
        f"module {module_name} has no default class; pass module.ClassName to target_type"
    )

Two consequences:

  1. The ValueError above, and its guidance to pass module.ClassName, never
    surfaces. Users see an AttributeError instead.
  2. The enclosing try only catches ValueError, so callers passing
    break_on_fail=False receive an exception rather than False.

This switches the read to a defaulted lookup so the existing error path works
as written. No behaviour changes for modules that do define DEFAULT_CLASS.

Reproduction

>>> _plugins.load_plugin("detectors.always", break_on_fail=False)
AttributeError: module 'garak.detectors.always' has no attribute 'DEFAULT_CLASS'

Also reachable from the CLI, e.g. --target_type base.

Testing

Adds test_load_plugin_module_without_default_class to
tests/plugins/test_plugin_load.py, covering both the break_on_fail=False
return value and the ValueError message. The test fails on main and passes
with this change. Full tests/plugins/ suite passes.

load_plugin read generator_mod.DEFAULT_CLASS directly, so a module without
that attribute raised AttributeError before the else branch could run. That
made the 'no default class; pass module.ClassName to target_type' ValueError
unreachable, and callers passing break_on_fail=False got an exception instead
of False. Use a defaulted lookup so the intended error path is reachable.

Signed-off-by: manunicholasjacob <manunicholasjacob@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant